package com.mrdeer.iot.rabbitmq;

import com.mrdeer.iot.bean.enums.EMQExchange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @Author: xie jianchu
 */
@Slf4j
@Component
public class MQDeviceEventSender {

    @Resource
    private AmqpTemplate amqpTemplate;

    public void send(EMQExchange[] mqExchange, String content) {
        Message message = MessageBuilder.withBody(content.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8")
                .build();
        if (mqExchange == null || mqExchange.length == 0) {
            return;
        }
        Arrays.asList(mqExchange).stream().forEach(emqExchange -> {
            log.info("send -> {} -> {}", emqExchange.name(), message);
            this.amqpTemplate.convertAndSend(emqExchange.getMqFanoutExchange(), "", message);
        });
    }

    /**
     * 使用直连交换机发送消息
     *
     * @param mqExchange
     * @param content
     */
    public void sendByDirectExchange(EMQExchange[] mqExchange, String content) {
        Message message = MessageBuilder.withBody(content.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8")
                .build();
        if (mqExchange == null || mqExchange.length == 0) {
            return;
        }
        Arrays.asList(mqExchange).stream().forEach(emqExchange -> {
            log.info("send -> {} -> {}", emqExchange.name(), message);
            this.amqpTemplate.convertAndSend("testDirectExchange", "testDirectRouting", message);
        });
    }

    /**
     * 使用主题交换机发送消息
     *
     * @param mqExchange
     * @param content
     */
    public void sendByTopicExchangeMan(EMQExchange[] mqExchange, String content) {
        Message message = MessageBuilder.withBody(content.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8")
                .build();
        if (mqExchange == null || mqExchange.length == 0) {
            return;
        }
        Arrays.asList(mqExchange).stream().forEach(emqExchange -> {
            log.info("send -> {} -> {}", emqExchange.name(), message);
            this.amqpTemplate.convertAndSend("topicExchange", "topic.man", message);
        });
    }

    /**
     * 使用主题交换机发送消息
     *
     * @param mqExchange
     * @param content
     */
    public void sendByTopicExchangeWoman(EMQExchange[] mqExchange, String content) {
        Message message = MessageBuilder.withBody(content.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8")
                .build();
        if (mqExchange == null || mqExchange.length == 0) {
            return;
        }
        Arrays.asList(mqExchange).stream().forEach(emqExchange -> {
            log.info("send -> {} -> {}", emqExchange.name(), message);
            this.amqpTemplate.convertAndSend("topicExchange", "topic.woman", message);
        });
    }

    /**
     * 使用主题交换机发送消息
     *
     * @param mqExchange
     * @param content
     */
    public void sendByFanoutExchangeWoman(EMQExchange[] mqExchange, String content) {
        Message message = MessageBuilder.withBody(content.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8")
                .build();
        if (mqExchange == null || mqExchange.length == 0) {
            return;
        }
        Arrays.asList(mqExchange).stream().forEach(emqExchange -> {
            log.info("send -> {} -> {}", emqExchange.name(), message);
            this.amqpTemplate.convertAndSend("fanoutExchange", "", message);
        });
    }
}
